<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    :title="form.id ? '编辑' : '新增'"
    width="600"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-align="right"
      label-width="110"
      label-position="top"
    >
      <el-row :gutter="20">
        <el-col :span="21">
          <el-form-item label="国家中文名" prop="name">
            <el-input v-model.trim="form.name" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="3" class="mt-7">
          <el-form-item label="" label-width="0" prop="status">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="2"
              active-text="启用"
              inactive-text="禁用"
              inline-prompt
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="国家英文名" prop="en_name">
            <el-input v-model.trim="form.en_name" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="国家二字简码" prop="country">
            <el-input v-model.trim="form.country" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="国家三字简码">
            <el-input v-model.trim="form.country_three" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="国家别称" prop="en_alias">
            <el-input v-model.trim="form.en_alias" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="TEMU国家ID">
            <el-input
              v-model.trim="form.temu_country_id"
              placeholder="请输入"
              clearable
              oninput="value=value.replace(/[^0-9]/g,'')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="币种" prop="currency_id">
            <select-currency
              v-model="form.currency_id"
              placeholder="请选择"
              class="w-full"
            ></select-currency>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="语言" prop="language_id">
            <el-select
              v-model="form.language_id"
              clearable
              placeholder="请选择"
              class="w-full"
            >
              <el-option
                v-for="item in languageOption"
                :key="item.value"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序">
            <el-input
              v-model="form.sort"
              placeholder="请输入"
              oninput="value=value.replace(/[^0-9]/g,'')"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
import api from "../api";
import { reactive, toRefs, onMounted } from "vue";
import { ElMessage } from "element-plus";
import SelectCurrency from "@/components/SelectCurrency";
const emits = defineEmits(["query"]);
const props = defineProps({
  options: {
    type: Object,
    required: true,
  },
});
const data = reactive({
  form: {},
  visible: false,
  loading: false,
  formRef: null,
  currencyOption: [],
  languageOption: [],
  rules: {
    name: [{ required: true, message: "请输入国家中文名" }],
    en_name: [{ required: true, message: "请输入国家英文名" }],
    country: [{ required: true, message: "请输入国家二字简码" }],
    en_alias: [{ required: true, message: "请输入国家别称" }],
    currency_id: [{ required: true, message: "请选择币种", trigger: "change" }],
    language_id: [{ required: true, message: "请输入语言", trigger: "change" }],
  },
  load: false,
});
const {
  rules,
  form,
  visible,
  loading,
  formRef,
  currencyOption,
  languageOption,
  load,
} = toRefs(data);
const open = async (id) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  getCurrency();
  getLanguage();
  if (id) getDetail(id);
  visible.value = true;
};
const getDetail = (id) => {
  api.getDetail({ id }).then((res) => {
    form.value = res.data;
  });
};
const getCurrency = () => {
  api
    .getCurrency({
      pageNum: 1,
      pageSize: 9999,
    })
    .then((res) => {
      currencyOption.value = res.data;
    });
};
const getLanguage = () => {
  api
    .getLanguage({
      pageNum: 1,
      pageSize: 9999,
    })
    .then((res) => {
      languageOption.value = res.data;
    });
};
const save = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      if (form.value.id) {
        api
          .update(form.value)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        api
          .add(form.value)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    }
  });
};

const initForm = () => {
  form.value = getForm();
};
// 初始化
const getForm = () => {
  return {
    name: "",
    status: 1,
    en_name: "",
    country: "",
    en_alias: "",
    currency_id: "",
    currency_id: "",
  };
};

onMounted(() => {
  initForm();
});

const close = () => {
  initForm();
  visible.value = false;
};
defineExpose({ open });
</script>
